package com.D5Power.Editor.EditorWindow
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	import flash.utils.ByteArray;
	
	import org.aswing.JButton;
	import org.aswing.JLabel;
	import org.aswing.JPanel;
	import org.aswing.JTextField;
	import org.aswing.SoftBoxLayout;
	import org.aswing.ext.Form;

	public class CharacterPosConfiger extends D5EditorWin
	{
		private var main:JPanel;
		private var shower:Sprite;
		private var input:JTextField;
		private var path:String;
		
		/**
		 * 动作个数
		 */ 
		private var actions:uint;
		/**
		 * 动作方向数
		 */ 
		private var lines:uint;
		
		/**
		 * 每个动作的宽度
		 */ 
		private var perw:uint;
		
		/**
		 * 每个动作的高度
		 */
		private var perh:uint;
		/**
		 * 源数据
		 */ 
		private var _resource:Bitmap;
		
		private var _list:Vector.<Vector.<Shape>>;
		
		public function CharacterPosConfiger(owner:D5RpgEditor)
		{
			mytitle = '角色素材定位';
			super(owner);
		}
		
		override protected function init(mode:uint=0):void
		{
			_box = new Form();
			main = new JPanel(new SoftBoxLayout(SoftBoxLayout.Y_AXIS));
			
			var lab0:JLabel = new JLabel('输入规格(素材包含的动作及行数)：');
			
			input = new JTextField('',9);
			input.setToolTipText('格式为动作个数+英文逗号+行数，如9,5');
			
			var bt0:JButton = new JButton('打开');
			
			bt0.addActionListener(openFill);
			main.append(_box);
			getContentPane().append(main);
			
			_box.addRow(lab0,input,bt0);
			_box.setSizeWH(800,600);
			super.init();
		}
		
		override protected function initEnd(mode:uint=0):void
		{
			setSizeWH(_box.width+_padding*2,_box.height+_padding*2);
			setLocationXY(100,100);
		}
		
		private function openFill(e:Event):void
		{
			var t:String = input.getText();
			
			if(t=='' || t.split(',').length!=2)
			{
				_root.msg('提示','请填写素材规格，并确认素材规格符合要求！');
				return;
			}
			
			var arr:Array = t.split(',');
			actions = int(arr[0]);
			lines = int(arr[1]);
			if(actions<0 || lines<0) return;
			
			_list = new Vector.<Vector.<Shape>>();
			for(var i:uint=0;i<lines;i++)
			{
				var v:Vector.<Shape> = new Vector.<Shape>(actions);
				_list.push(v);
			}
			
			var file_open:File = File.desktopDirectory;
			file_open.browseForOpen('打开角色素材', [new FileFilter('图片', '*.jpg;*.gif;*.png' )] );
			file_open.addEventListener(Event.SELECT,onOpen);
		}
		
		private function onOpen(e:Event):void
		{
			var file:File = e.target as File;
			path = file.nativePath;
			var fs:FileStream = new FileStream();
			var bytes:ByteArray = new ByteArray();
			fs.open(file,FileMode.READ);
			fs.readBytes(bytes,0,fs.bytesAvailable);
			fs.close();
			
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplate);
			loader.loadBytes(bytes);
		}
		
		private function onComplate(e:Event):void
		{
			var target:LoaderInfo = e.target as LoaderInfo;
			_resource = target.loader.content as Bitmap;
			
			shower = new Sprite();
			shower.addChild(_resource);
			shower.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
			shower.addEventListener(MouseEvent.MOUSE_UP,onUp);
			shower.addEventListener(MouseEvent.CLICK,onClick);
			
			var grid:Shape = new Shape();
			grid.graphics.lineStyle(1);
			
			perw = int(_resource.width/actions);
			perh = int(_resource.height/lines);
			
			for(var l:uint = 0;l<lines;l++)
			{
				grid.graphics.moveTo(0,l*perh);
				grid.graphics.lineTo(_resource.width,l*perh);
				
				for(var a:uint = 0;a<actions;a++)
				{
					grid.graphics.moveTo(a*perw,0);
					grid.graphics.lineTo(a*perw,_resource.height);
				}
			}
			shower.addChild(grid);
			
			
			main.addChild(shower);
			
			main.removeAll()
			
		}
		
		private function onDown(e:MouseEvent):void
		{
			shower.startDrag();	
		}
		private function onUp(e:MouseEvent):void
		{
			shower.stopDrag();
		}
		private function onClick(e:MouseEvent):void
		{
			if(!e.ctrlKey) return;
			var atX:uint = int(e.localX/perw);
			var atY:uint = int(e.localY/perh);
			
			if(_list[atY][atX]!=null)
			{
				_list[atY][atX].x = e.localX;
				_list[atY][atX].y = e.localY;
				return;
			}
			
			var point:Shape = new Shape();
			point.graphics.lineStyle(1);
			point.graphics.beginFill(0xff0000);
			point.graphics.drawCircle(0,0,5);
			_list[atY][atX] = point;
			shower.addChild(point);
			
		}
	}
}